package com.whfc.ms.api.quality.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.quality.service.MsQualityRectifyService;
import com.whfc.ms.config.AbstractBaseController;
import com.whfc.quality.dto.QualityCheckDTO;
import com.whfc.quality.dto.QualityRectifyDTO;
import com.whfc.quality.param.QualityRectifyAddParam;
import com.whfc.quality.param.QualityRectifyEditParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author: hw
 * @date: 2021-10-19 15:39
 * @description: 质量整改单
 */
@RequiresPermissions("quality/rectify")
@RestController
@RequestMapping("/ms/api/quality/rectify")
public class QualityRectifyController extends AbstractBaseController {

    @Autowired
    private MsQualityRectifyService msQualityRectifyService;

    @GetMapping("list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                       @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
                       @RequestParam(value = "pageNum") Integer pageNum,
                       @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("整改通知单列表,loginer:{},deptId:{},startTime:{},endTime:{},pageNum:{},pageSize{}",
                user.getUsername(), deptId, startTime, endTime, pageNum, pageSize);
        PageData<QualityRectifyDTO> list = msQualityRectifyService.list(deptId, startTime, endTime, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @GetMapping("details")
    public Result details(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("rectifyId") Integer rectifyId,
                          @RequestParam(value = "pageNum") Integer pageNum,
                          @RequestParam(value = "pageSize") Integer pageSize) {
        logger.info("整改通知单详情,loginer:{},rectifyId:{},pageNum:{},pageSize{}",
                user.getUsername(), rectifyId, pageNum, pageSize);
        PageData<QualityCheckDTO> list = msQualityRectifyService.details(rectifyId, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    @RequiresPermissions("quality/rectify/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody QualityRectifyAddParam param) {
        logger.info("添加整改通知单,loginer:{},param:{}", user.getUsername(), param.toString());
        param.setCreateUserId(user.getId());
        param.setCreateUserName(user.getNickname());
        msQualityRectifyService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("quality/rectify/add")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @Validated @RequestBody QualityRectifyEditParam param) {
        logger.info("修改整改通知单,loginer:{},param:{}", user.getUsername(), param.toString());
        msQualityRectifyService.edit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("quality/rectify/del")
    @PostMapping("del/{rectifyId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @PathVariable("rectifyId") Integer rectifyId) {
        logger.info("删除整改通知单,loginer:{},rectifyId:{}", user.getUsername(), rectifyId);
        msQualityRectifyService.del(rectifyId);
        return ResultUtil.success();
    }


    @GetMapping("export")
    public Result export(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("rectifyId") Integer rectifyId) {
        logger.info("整改通知单详情,loginer:{},rectifyId:{}",
                user.getUsername(), rectifyId);
        msQualityRectifyService.export(rectifyId);
        return ResultUtil.success();
    }

    /**
     * 2.0质量整改通知单批量删除
     *
     * @param idList
     * @return
     */
    @RequiresPermissions("quality/rectify/del")
    @PostMapping("/delete/batch")
    public R deleteBatch(@RequestBody List<Integer> idList) {
        logger.info("质量整改通知单批量删除,userId:{},idList:{}", getLoginUser().getId(), idList);
        AssertUtil.isTrue(CollectionUtils.isEmpty(idList), "ID不能为空");
        msQualityRectifyService.deleteBatch(idList);
        return R.success();
    }

}
